package com.weng.coder.service.generateCode;

import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import java.io.Writer;
import java.util.Map;
import org.springframework.stereotype.Service;

import com.weng.coder.common.model.File;


/**
 * 从文本读取Freemarker模板并处理
 * 
 * @author wengzhonghui
 */
@Service("contentFreemarkerProcessor")
public class ContentFreemarkerProcessorImpl implements FreemarkerProccesor {

    @Override
    public void processTemplate(File file, Map freemarkerModel, Writer writer) {
        try {
            Configuration configuration = new Configuration();
            configuration.setObjectWrapper(new DefaultObjectWrapper());
            configuration.setOutputEncoding("UTF8");
            configuration.setDefaultEncoding("UTF8");

            StringTemplateLoader loader = new StringTemplateLoader();
            loader.putTemplate(file.getName(), file.getContent());
            configuration.setTemplateLoader(loader);
            freemarker.template.Template template = configuration.getTemplate(file.getName(), "UTF8");
            template.process(freemarkerModel, writer);
        } catch (Exception e) {
        	e.printStackTrace();
        }
    }
}
